package com.example.democlient.network;


import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.SocketChannel;
import java.util.Timer;

import android.app.Activity;
import android.os.Handler;
import android.util.Log;

import com.example.democlient.MainActivity;
import com.example.democlient.MyApplication;
import com.example.democlient.network.handler.PacketHandlerFactory;
import com.example.democlient.network.packet.ServerPacket;
import com.example.democlient.network.timeout.IdleHandler;


public class SocketService
{
	public ServerConnection serverConn;

	//public static String ipAddress = "175.162.116.178";
	public static String ipAddress = "59.108.47.58";
	
	public static final SocketService getInstance()
	{
		return SingletonHolder.instance;
	}

	private SocketService()
	{
	}
	
	private static class SingletonHolder
	{
		protected static final SocketService instance = new SocketService();
	}
	
	final static SocketAddress address = new InetSocketAddress(ipAddress,8888);
	
	Runnable runnable = new Runnable(){
	    @Override
	    public void run() {
	    	serverConn=connect();
	    }
	};
	Runnable restart = new Runnable(){
	    @Override
	    public void run() {
	    	serverConn=connect();
	    	if(serverConn!=null){
	    		serverConn.sendPacket(bp, myApp);
	    	}else{
	    		bp=null;
	    		myApp=null;
	    	}
	    }
	};
	public void startup(){
		new Thread(runnable).start();
	}
	ServerPacket bp;MyApplication myApp;
	public boolean sendPacket(ServerPacket bp,MyApplication myApp) {
		if(serverConn==null){
			this.bp = bp;
			this.myApp = myApp;
			new Thread(restart).start();
//			Handler hander = this.serverConn.app.getHander(calssz);
//	    	hander.sendEmptyMessage(999);
//			this.startup();
//			if(serverConn!=null) {
//				serverConn.sendPacket(bp, myApp);
//				return true;
//			}
			return false;
		}else{
			serverConn.sendPacket(bp, myApp);
			return true;
		}
	}
	
	private static final Class<? extends Activity> calssz = MainActivity.class;
	public void restart(){
		if(this.serverConn!=null){
			if(this.serverConn.app!=null){
				Handler hander = this.serverConn.app.getHander(calssz);
		    	hander.sendEmptyMessage(999);
			}
			serverConn.onServerClose();
			this.d=null;
			this.packetHandlerFactory = null;
			this.serverConn=null;
			System.gc();
		}
		this.serverConn=this.connect();
	}
	
	Dispatcher d;
	PacketHandlerFactory packetHandlerFactory;
	
	public ServerConnection connect () 
	{
		SocketChannel sc;
		Log.i("connect","正在连接到登陆服务器.");
		try
		{
			sc = SocketChannel.open(address);
			sc.configureBlocking(false);
			d = new Dispatcher();
			packetHandlerFactory = new PacketHandlerFactory();
			Timer timer = new Timer();
			IdleHandler idle = new IdleHandler(timer, 200, 50);
			idle.initialize(this);
			return new ServerConnection(sc, d, packetHandlerFactory.getPacketHandler());
		}
		catch(Exception e)
		{
			e.printStackTrace();
			Log.i("info","无法连接到服务器: " + e.getMessage());
			return null;
		}
			
	}


}
